package com.category.controller;

import com.category.common.Result;
import com.category.entity.DateGroup;
import com.category.service.DateGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
@CrossOrigin
public class DateGroupController {
    
    @Autowired
    private DateGroupService dateGroupService;
    
    @GetMapping("/list/{date}")
    public Result<List<DateGroup>> getGroupsByDate(@PathVariable String date) {
        List<DateGroup> groups = dateGroupService.getGroupsByDate(date);
        return Result.success(groups);
    }
    
    @GetMapping("/all")
    public Result<List<DateGroup>> getAllGroups() {
        List<DateGroup> groups = dateGroupService.getAllGroups();
        return Result.success(groups);
    }
    
    @PostMapping("/add")
    public Result<String> addGroup(@RequestBody DateGroup dateGroup) {
        try {
            dateGroupService.addGroup(dateGroup);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteGroup(@PathVariable Long id) {
        boolean success = dateGroupService.deleteGroup(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }
}
